/**
 * Created by Daniel on 2017/9/8.
 */
var qiniu = require("qiniu");

var bucket = 'family';
var options = {
    scope: bucket,
    // callbackUrl: "http://systemcrm.leanapp.cn/editor/uploadSuccess",
    // callbackUrl: "http://api.example.com/qiniu/upload/callback",
    callbackUrl:"http://tribe.aladada.com/qiniuUploadSuccess",
    callbackBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}',
    callbackBodyType: 'application/json',
    expires: 3600,
};

var accessKey = 'AsPvgPQZqIPBojLgMm6HixsjsIz7YCEy1gNli9Zb';
var secretKey = 'cJ47G1jJPzC20260pxlt9uh4ScUV51MlU7iMvhC9';
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

//生成token
// var uploadToken=putPolicy.uploadToken(mac);

var config = new qiniu.conf.Config();
// 空间对应的机房
config.zone = qiniu.zone.Zone_z0;
// 是否使用https域名
//config.useHttpsDomain = true;
// 上传是否使用cdn加速
//config.useCdnDomain = true;
// var localFile = "C:/Users/daniel/pictures/Windows 照片查看器墙纸.jpg";
// var localFile = "../aaa.png";
var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
// var key='my-nodejs-logo2.jpg';
// 文件上传
exports.uploadImg = uploadImg;
function uploadImg(localFile,key,callback) {
    var putPolicy = new qiniu.rs.PutPolicy(options);
    var uploadToken=putPolicy.uploadToken(mac);
    formUploader.putFile(uploadToken, key, localFile, putExtra, function(respErr,respBody,respInfo) {
        if (respErr) {
            throw respErr;
            callback(null,"201");//错误
        }
        if (respInfo.statusCode == 200) {
            // console.log(respBody);
            // console.log(JSON.stringify(respInfo));
            callback(null,"200");//成功
        } else {
            // console.log(respInfo.statusCode);
            // console.log(respBody);
            callback(null,"203");//上传失败
        }
    });
}


//js上传需要的token
exports.config={
    'AccessKey': 'AsPvgPQZqIPBojLgMm6HixsjsIz7YCEy1gNli9Zb',  // https://portal.qiniu.com/user/key
    'SecretKey': 'cJ47G1jJPzC20260pxlt9uh4ScUV51MlU7iMvhC9',
    'Bucket': 'family',
    'Port': 19110,
    'UptokenUrl': 'uptoken',
    'Domain': 'http://tribemedia.aladada.com/' // bucket domain eg:http://qiniu-plupload.qiniudn.com/
};

exports.liveConfig={
    'AccessKey': 'AsPvgPQZqIPBojLgMm6HixsjsIz7YCEy1gNli9Zb',  // https://portal.qiniu.com/user/key
    'SecretKey': 'cJ47G1jJPzC20260pxlt9uh4ScUV51MlU7iMvhC9',
    'Bucket': 'buluoquan',
    'Port': 19110,
    'UptokenUrl': 'uptoken',
    'Domain': 'http://buluoquan.aladada.com/', // bucket domain eg:http://qiniu-plupload.qiniudn.com/
    'noneHttpDomain': 'buluoquan.aladada.com'
};

